<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="资产名称" prop="assetsName">
            <el-input v-model="queryParams.assetsName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="资产类型" prop="assetsType">
            <el-select v-model="queryParams.assetsType" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_assets_type" :key="dict.value" :label="dict.label"
                         :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="资产ID" prop="assetsIdCard">
            <el-input v-model="queryParams.assetsIdCard" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="报废状态" prop="scrappedType">
            <el-select v-model="queryParams.scrappedType" clearable placeholder="请选择"
                       @keyup.enter.native="handleQuery">
              <el-option label="待审核" value="1"></el-option>
              <el-option label="已报废" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['cld:scrapped:add']" icon="el-icon-plus" plain size="mini" type="primary"
                   @click="handleAddOrView">新增报废
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="recordList" border>
      <el-table-column align="center" label="资产名称" prop="assetsName"/>
      <el-table-column align="center" label="资产类型" prop="assetsType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_assets_type" :value="scope.row.assetsType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="资产ID" prop="assetsIdCard"/>
      <el-table-column align="center" label="资产图片" prop="assetsImg">
        <template slot-scope="scope">
          <image-preview :height="50" :src="scope.row.assetsImg" :width="50"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="资产型号" prop="assetsModel"/>
      <el-table-column align="center" label="资产单位" prop="assetsUnit"/>
      <el-table-column align="center" label="报废状态">
        <template slot-scope="scope">
          <el-tag v-if="!scope.row.scrapApproveTime" type="success">待审核</el-tag>
          <el-tag v-if="scope.row.scrapApproveTime" type="danger">已报废</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="资产单价" prop="bulMoney">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.addedMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="累计折旧金额" prop="depreciationMoney">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.addedMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="累计维修金额" prop="reportMoney">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.addedMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="累计增值金额" prop="addedMoney">
        <template slot-scope="scope">
          <span>{{ toMoney(scope.row.addedMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="审批人" prop="scrapApproveName"/>
      <el-table-column align="center" label="审批时间" prop="scrapApproveTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scrapApproveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-hasPermi="['cld:scrapped:audit']" v-if="!scope.row.scrapApproveTime" size="mini" type="text"
                     @click="handleAudit(scope.row)">审核
          </el-button>
          <el-button size="mini" type="text"
                     @click="handleRepairDetails(scope.row)">折旧明细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>

    <!-- 添加或修改资产折旧对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="选择设备" prop="assetsIdCard">
              <el-input v-model="form.assetsIdCard" :disabled="true" placeholder="请选择">
                <el-button slot="append" :disabled="isAudit" icon="el-icon-search"
                           @click="handleAddAssets">选择
                </el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报废时间" prop="scrapTime">
              <el-date-picker v-model="form.scrapTime" :disabled="isAudit" type="datetime" clearable
                              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="报废描述" prop="scrapDesc">
              <el-input v-model="form.scrapDesc" :disabled="isAudit" :rows="4" placeholder="请输入" type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!isAudit" type="primary" @click="submitForm">确 定</el-button>
        <el-button v-if="isAudit" type="primary" @click="submitAudit(2)">通 过</el-button>
        <el-button v-if="isAudit" type="primary" @click="submitAudit(1)">拒 绝</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择资产 -->
    <el-dialog :close-on-click-modal="false" :destroy-on-close="true" :visible.sync="isAssetsOpen" append-to-body title="选择资产"
               width="60%">
      <selected-assets v-if="isAssetsOpen" @change="handleAssetsChange">
      </selected-assets>
    </el-dialog>

    <!-- 折旧明细 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="depreciationOpen" append-to-body title="折旧明细"
               width="800px">
      <el-table v-loading="loading" :data="detailsList">
        <el-table-column align="center" label="项目区域" prop="projectRegion">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称" prop="projectName"/>
        <el-table-column align="center" label="折旧期数" prop="periodNumber"/>
        <el-table-column align="center" label="折旧时间">
          <template slot-scope="scope">
            {{ scope.row.year + '-' + scope.row.month + '-' + scope.row.day }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="折旧/盈利" prop="lossType">
          <template slot-scope="scope">
            {{ scope.row.lossType == 1 ? '折旧' : '盈利' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="涉及金额">
          <template slot-scope="scope">
            {{ scope.row.lossType == 1 ? toMoney(scope.row.depreciationMoney) : toMoney(scope.row.addedMoney) }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {addPlacementRecord, getRecord, listRecord, updateRecordScrap} from "@/api/cld/record";
import {listDepreciation} from "@/api/cld/depreciation";
import selectedAssets from "../selectedAssets.vue";

export default {
  name: "Depreciation",
  components: {
    selectedAssets
  },
  dicts: ['cld_assets_type', 'cld_project_region'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产报废表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        recordStatus: 4,
        assetsName: null,
        assetsIdCard: null,
        assetsType: null,
        scrappedType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      isAssetsOpen: false,
      depreciationOpen: false,
      // 折旧明细
      detailsList: [],
      // 明细查询参数
      detailsQueryParams: {
        pageNum: 1,
        pageSize: 1000,
        assetsIdCard: null,
        params: {
          groupType: 1
        }
      },
      // 是否审核
      isAudit: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产折旧列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordStatus: 4,
        scrapTime: new Date()
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAddOrView() {
      this.reset();
      this.open = true;
      this.title = "新增资产报废";
    },
    // 审核
    handleAudit(row) {
      this.reset();
      this.isAudit = true;
      getRecord(row.assetsRecordId).then(res => {
        this.form = res.data;
        this.open = true;
        this.title = "资产报废";
      })
    },
    // 审核提交
    submitAudit(status) {
      let form = {
        assetsRecordId: this.form.assetsRecordId,
        scrapStatus: status
      }
      updateRecordScrap(form).then(res => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.isAudit = false;
        this.getList();
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addPlacementRecord(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 折旧明细 */
    handleRepairDetails(row) {
      this.detailsList = [];
      this.detailsQueryParams.assetsIdCard = row.assetsIdCard;
      listDepreciation(this.detailsQueryParams).then(response => {
        this.detailsList = response.rows;
        this.detailsTotal = response.total;
        this.depreciationOpen = true;
      })
    },
    // 选择资产
    handleAddAssets() {
      this.isAssetsOpen = true;
    },
    // 确认选择资产
    handleAssetsChange(e) {
      this.form.assetsIdCard = e.assetsIdCard;
      this.form.assetsRecordId = e.assetsRecordId;
      this.isAssetsOpen = false;
    },
  }
};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
</style>
